package com.sen.common.widget;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

/**
 * HTML控件的基本属性.
 * 
 * @author sen
 */
public class Attribute implements ToHtmlable {

	private String id;
	private String name;
	private String className;
	private String width;
	private String height;
	private Style style;
	private String title;
	private String caption;
	private Map<String, String> attrMap = new HashMap<String, String>();

	public Attribute id(String id) {
		this.id = id;
		return this;
	}

	public Attribute name(String name) {
		this.name = name;
		return this;
	}

	public Attribute className(String className) {
		this.className = className;
		return this;
	}

	public Attribute width(String width) {
		this.width = width;
		return this;
	}

	public Attribute height(String height) {
		this.height = height;
		return this;
	}

	public Attribute style(String style) {
		this.style = new Style(style);
		return this;
	}

	public Attribute style(String styleName, String styleValue,
			String... others) {
		this.style = new Style(styleName, styleValue, others);
		return this;
	}

	public Attribute style(final Map<String, String> styles) {
		this.style = new Style(styles);
		return this;
	}

	public Attribute title(String title) {
		this.title = title;
		return this;
	}

	public Attribute caption(String caption) {
		this.caption = caption;
		return this;
	}

	public Attribute attr(String attrName, String attrValue) {
		this.attrMap.put(attrName, attrValue);
		return this;
	}

	public Attribute merge(Attribute attribute) {
		if (attribute == null) {
			return this;
		}
		id = StringUtils.defaultString(attribute.id, id);
		name = StringUtils.defaultString(attribute.name, name);
		className = StringUtils.defaultString(attribute.className, className);
		width = StringUtils.defaultString(attribute.width, width);
		height = StringUtils.defaultString(attribute.height, height);
		style = attribute.style == null ? style : attribute.style;
		title = StringUtils.defaultString(attribute.title, title);
		caption = StringUtils.defaultString(attribute.caption, caption);
		attrMap.putAll(attribute.attrMap);
		return this;
	}

	public String toHtml() {
		StringBuffer html = new StringBuffer();
		html.append(attrIfNonblank("id", id)).append(
				attrIfNonblank("name", name)).append(
				attrIfNonblank("class", className)).append(
				attrIfNonblank("width", width)).append(
				attrIfNonblank("height", height)).append(styleIfNonblank())
				.append(attrIfNonblank("title", title)).append(
						attrIfNonblank("caption", caption)).append(
						otherCustomizedAttrs());
		return html.toString();
	}

	private String attrIfNonblank(String attr, String value) {
		return Attributes.attrIfNonblank(attr, value);
	}

	private String styleIfNonblank() {
		if (style == null) {
			return StringUtils.EMPTY;
		}
		return attrIfNonblank("style", style.toHtml());
	}

	private String otherCustomizedAttrs() {
		if (MapUtils.isEmpty(attrMap)) {
			return StringUtils.EMPTY;
		}
		StringBuffer html = new StringBuffer();
		for (Map.Entry<String, String> attr : attrMap.entrySet()) {
			html.append(Attributes.attr(attr.getKey(), attr.getValue()));
		}
		return html.toString();
	}

}
